<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\DB;


class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    public function index(Request $request)
    {
        $userser = DB::table('gie')->get();
        $value = $request->session()->get('username');
        $user = (DB::table('user')->where('username',$value)->first())->baomu ?? '';
        // dd($user);
        $userser = json_decode(json_encode($userser), true);
        // $keys = array_keys($userser);
        for($a=0;$a<count($userser);$a++){
            $key[$userser[$a]['name']] = $userser[$a]['id'];
        }
        // dd($key);
    	return view('index',['userser'=>$key,'user'=>$user]);
    }
	//个人中心显示
	public function index_center(Request $request)
	{
        $value = $request->session()->get("username");
        $rwos = Db::table("user")->where('username',$value)->first();
        $res = json_decode(json_encode($rwos),true);
        $vip = $res['vip'];
		return view('index_center')->with('vip',$vip);
	}
    //订单
    public function myorder(Request $request)
    {
        $value = $request->session()->get('username');
        $users = DB::table('dindan')->where('user_id',$value)->get();
    	return view('myorder');
    }

    public function cleaning_index(Request $request)
    {
        $rows =$request->all();
        if(empty($rows['ids'])){
            $search = '';
        }else{
            $search =$rows['ids'];
        }
        $userser = DB::table('site')->where('id',$search)->first();
    	$users = DB::table('gie')->where('id',$rows['id'])->first();
    	$kk = json_decode($users->selest,true);
        // dd($userser);
        $keys = array_keys($kk);
        for($a=0;$a<count($kk);$a++){
            $zhi[$a]['id'] =$keys[$a];
            $zhi[$a]['zhi'] =explode(',',$kk[$keys[$a]]);
        }
        // dd($zhi);
    	// return view('cleaning_index')->with('userk',$userk)->with('userss',$userss)->with('userser',$userser);
        return view('cleaning_index')->with('zhi',$zhi)->with('userser',$userser)->with('users',$users);
    }

    public function cleaning_er(Request $request)
    {
        $row =$request->all();
        $rows = json_encode($row['p'],true);
        if(count($row['p'])==$row['l']){
        $users = DB::table('kucun2')->where('class',$rows)->first();
        $kk = json_decode($users->jiage,true);
        // dd($kk);
        return json_encode(['jiage'=>$kk],JSON_UNESCAPED_UNICODE);
        }
        // return json(['jiage'=>$row['ries'],'maob'=>$row['kick'],'ids'=>$row['id']]);
    
    }

    public function address(Request $request)
    {
        $id =$request->all();
        $chaxun = Db::table('gie')->where('id',$id['id'])->first();
        // dd($chaxun);
        $value = $request->session()->get('username');
    	if($value==null){
    	    return redirect('login');
    	 }else{
        $ssook = DB::table('user')->where('username',$value)->first();
        $kk = json_decode($ssook->id,true);
        $users = DB::table('site')->where('user_id',$kk)->get();
        return view('address')->with('users',$users)->with('chaxun',$chaxun);
    	}
    }

    public function address_add(Request $request)
    {
        $ssk =$request->all();
        if(empty($ssk['ids'])){
            $search = '';
        }else{
            $search =$ssk['ids'];
        }
    
        $users = DB::table('dinwei')->where('id',$search)->first();
        return view('address_add')->with('users',$users);
    }
    
    public function adders(Request $request)
    {
        $value = $request->session()->get('username');
        if($value==null){
            return redirect('login');
         }else{
        $ssook = DB::table('user')->where('username',$value)->first();
        $oopp = json_decode($ssook->id,true);
        $rows =$request->all();
        $shu = count($rows['data']);
        for($a=0;$a<$shu;$a++){
            $shuju[$rows['data'][$a]['name']] = $rows['data'][$a]['value'];
        }
        $shuju['username']=$shuju['user_id'];
        unset($shuju['user_id']);
        $shuju['site_id'] =$rows['ss'];
        $shuju['user_id'] =$oopp;
    
        // dump($shuju);die();
        // $row['site_id'] = $rows['ss'];
        $add = Db::table('site')->insert($shuju);
        if($add){
        return response()->json(['code'=>1 , 'msg'=>'1']);
        }else{
        return json_encode(['code'=>0 , 'msg'=>'0']);
        }
        }
    }

	public function floor(Request $request)
	{
	    $rows =$request->all();
	    if(empty($rows['ids'])){
	        $search = '';
	    }else{
	        $search =$rows['ids'];
	    }
	    $userser = DB::table('site')->where('id',$search)->first();
	    $users = DB::table('gie')->where('id',$rows['id'])->first();
	    $kk = json_decode($users->selest,true);
	    $keys = array_keys($kk);
	    for($a=0;$a<count($kk);$a++){
	        $zhi[$a]['id'] =$keys[$a];
	        $zhi[$a]['zhi'] =explode(',',$kk[$keys[$a]]);
	    }
		return view('floor',['userser'=>$userser,'zhi'=>$zhi,'users'=>$users]);
	}
	
	public function floor_er(Request $request)
	{
	    $row =$request->all();
	    $rows = json_encode($row['p'],true);
	    if(count($row['p'])==$row['l']){
	        $users = DB::table('kucun2')->where(['fu'=>$row['serve'],'class'=>$rows])->first();
	        $kk = json_decode($users->jiage,true);
	        return json_encode(['jiage'=>$kk],JSON_UNESCAPED_UNICODE);
	    }
	}

    public function shanchu(Request $request)
    {
        $rows =$request->all();
        $ppp =DB::table('site')->where('id',$rows)->delete();
        if($ppp){
        return response()->json(['code'=>1 , 'msg'=>'1']);
        }else{
        return response()->json(['code'=>0 , 'msg'=>'0']);
        }
    }

    public function tjia(Request $request)
    {
        $rows =$request->all();
        if($rows['xiadan_site']==""){
            return response()->json(['code'=>2 , 'msg'=>'请填写地址']);
        }else{
        $rows['guige'] = json_encode($rows['p'],true);
        // dd($rows);
        $rows['user_id'] = $request->session()->get('username');
        $rows['xiadan_titme'] = $rows['shijian'].' '.$rows['shijianer'];
        unset($rows['shijianer']);
        unset($rows['shijian']);
        // $rows['Xiadan_time'] = json_encode($rows['shijian'],true);
        unset($rows['p']);//删除变量里的数据
        $ssook = DB::table('kucun2')->where(['class'=>$rows['guige'],'fu'=>$rows['serve']])->select('id')->first();
        $rows['xiadan_guige'] = $ssook->id ;
        unset($rows['guige']);
        // dd($rows);
        $add = Db::table('dindan')->insertGetId($rows);
        if($add){
            return response()->json(['code'=>1 , 'msg'=>'1' , 'uiu'=>$add ]);
            }else{
            return response()->json(['code'=>0 , 'msg'=>'0']);
            }
        }
    }

    public function affirm_order(Request $request)
        {
            $rows =$request->all();
            $users = DB::table('dindan')->where('id',$rows)->first();
            $jiaq = DB::table('dindan')->where('id',$rows)->select('xiadan_money')->first();
            $oopp = json_decode($users->xiadan_guige,true);
            $userss = DB::table('kucun2')->where('id',$oopp)->first();
            $oopper = json_decode($users->xiadan_site,true);
            $kkll = DB::table('site')->where('id',$oopper)->first();
    
            $shanp = DB::table('shanp')->get();
            $yi = json_decode($userss->class,true);
            $er = array_keys($yi);
            $kkoo=[];
            for($a=0;$a<count($er);$a++){
                $pan = (Db::table('guige')->where('id',$er[$a])->first())->guige;
                if($pan!='时间'){
                $kkoo[$a]['id'] = (Db::table('guige')->where('id',$er[$a])->first())->guige;
                $kkoo[$a]['zhi'] = $yi[$er[$a]];
                }
            }
    // dd($kkoo);
            return view('affirm_order')->with('userss',$userss)->with('kkll',$kkll)->with('users',$users)->with('kkoo',$kkoo)->with('shanp',$shanp)->with('jiaq',$jiaq);
        }
    
    public function sheet(Request $request)
    {
        $value = $request->session()->get('username');
        if($value==""){
            return redirect('login');
        }
        $rows =$request->all();
        $oopp = $rows['id'];
        $rowse = $rows["xiadan_money"];
        $valuesan =  (DB::table('user')->where('username',$value)->first())->id;
        $valueer =  (DB::table('user')->where('username',$value)->first())->balance;
        if($valueer>=$rowse){
            $kk = $valueer-$rowse;
            $xiugai = DB::table('user')->where('id',$valuesan)->update(['balance'=>$kk]);
            $xiugaier = DB::table('dindan')->where('id',$oopp)->update(['xiadan_money'=>$rowse]);
            return response()->json(['code'=>1 , 'msg'=>'购买成功']);
        }else{
            return response()->json(['code'=>0 , 'msg'=>'余额不足请先充值']);
        }
    }
	
	public function baomu(Request $request)
	{
	    return view('baomu');
	}

	public function member()
	{
		return view('member');
	}
    //订单详情
    public function order()
    {

    	return view('order');
    }

    public function address_search()
    {
        $users = Db::table('dinwei')->get();
    	return view('address_search')->with('users',$users);
    }
    //显示登陆和注册页面
    public function login_index()
    {
        return view("login_index");
    }
    //登陆页面显示
    public function login()
    {
        return view("login");
    }
    //登陆
    public function login_ok(Request $request)
    {
        $name = $request->input('username');
        $pwd = $request->input('password');
        $user = Db::table('user')->where([
            ['username',$name],
            ['password',$pwd],
        ])->first();
        if($user){
            session(['username' => $name ]);
            return json_encode(['code'=>1 , 'msg'=>'登陆成功']);
        }else{
            return json_encode(['code'=>0 , 'msg'=>'用户名或密码错误']);
        }
    }
    //注册页面显示
    public function register()
    {
        return view("register");
    }
    //注册
    public function register_ok(Request $request)
    {
        $rows = $request->all();
        unset($rows['_token']);
        $res = Db::table("user")->where("username",$rows['username'])->first();
       if($res == null){
              $add = Db::table('user')->insert($rows);
              if($add){
                  return json_encode(['code'=>1 , 'msg'=>'注册成功']);
              }

       }else{
           return json_encode(['code'=>-1 , 'msg'=>'已有该用户']);
       }
    }
    //钱包显示
    public function money(Request $request)
    {
        $value = $request->session()->get('username');
        $rows = Db::table("user")->where("username",$value)->first();
        return view("money",['balance'=>$rows->balance]);
    }

    //充值显示
    public function recharge(Request $request)
        {
            $value = $request->session()->get("username");
            $rows = Db::table("user")->where("username",$value)->first();
            return view("recharge",["tel" => $rows->tel , 'yao' => $rows->yao]);
        }

    //充值会员
//    public function cwhy(Request $request)
//    {
//        $value = $request->session()->get("username");
//        $rows = Db::table("user")->where("username",$value)->get();
//        $res =json_decode(json_encode($rows), true);
//        if($res[0]['balance'] >= 30){
//            $rea = $res[0]["balance"] - 30;
//            if ($rea >= 0) {
//                $ret = DB::table("user")->where($rea[0]['id'])->update(['balance'=>$rea]);
//                if($ret){
//                    echo '充值成功';
//                }else{
//                    echo "充值失败";
//                }
//            }
//        }else{
//            echo "您的余额不够，请充值";
//        }
//    }

    //充值成功
    public function recharge_ok(Request $request)
    {
        $res = $request->all();
        $value = $request->session()->get("username");
        $rwos = Db::table('user')->where("username",$value)->get();
        $rpl = json_decode(json_encode($rwos),true);
        if($rpl[0]["yao"] == null){
            if($res["tel"] == null){

            }else {
                $tel = $res["tel"];
                $yao = Db::table('user')->where('tel', $tel)->first("tel");
                if ($yao || $yao == null) {
                    $tt = $yao;
                    $tuijain = Db::table('user')->where('username', $value)->update(['yao' => $tt->tel]);
                } else {
                    return json_encode(['code' => -3, 'msg' => '推荐人有误']);
                }
            }
        }else{
            if($res["tel3"] == $rpl[0]["yao"]){

            }
        }
        if($res['shuju'] >= 100 && $res['shuju'] <= 200000){
            $rex = $res['shuju'] + $rpl[0]["balance"];
            if($rpl[0]['unused'] == 0){
                $pl = $rex +100;
                $re = $rpl[0]["unused"] + $pl;
                $rows = Db::table('user')->where('username', $value)->update(['balance' => $pl, 'unused' => $re]);
                if ($rows) {
                    return json_encode(['code' => 1, 'msg' => '恭喜，大哥充值成功']);
                } else {
                    return json_encode(['code' => -2, 'msg' => '大哥刚刚充值出错了，您可以在试试吗？']);
                }
            }else {
                $re = $rpl[0]["unused"] + $res['shuju'];
                $rows = Db::table('user')->where('username', $value)->update(['balance' => $rex, 'unused' => $re]);
                if ($rows) {
                    $rom = Db::table('user')->where('username',$value)->get();
                    $vip = json_decode(json_encode($rom),true);
                    if($vip[0]["vip"] >= 6){
                        return json_encode(['code' => 1, 'msg' => '恭喜，大哥充值成功']);
                    }else{
                        if($vip[0]["unused"] >= 0 && $vip[0]["unused"] <= 20000){
                             return json_encode(['code' => 1, 'msg' => '恭喜，大哥充值成功']);
                        }else if($vip[0]["unused"] >= 20000 && $vip[0]["unused"] <= 40000 ){
                            $vip = $vip[0]["vip"] = 1;
                            $rt =Db::table('user')->where("username",$value)->update(['vip' => $vip]);
                        }else if($vip[0]["unused"] >= 40000 && $vip[0]["unused"] <= 70000 ){
                            $vip = $vip[0]["vip"] = 2;
                            $rt =Db::table('user')->where("username",$value)->update(['vip' => $vip]);
                        }else if($vip[0]["unused"] >= 70000 && $vip[0]["unused"] <= 110000 ){
                            $vip = $vip[0]["vip"] = 3;
                            $rt =Db::table('user')->where("username",$value)->update(['vip' => $vip]);
                        }else if($vip[0]["unused"] >= 110000 && $vip[0]["unused"] <= 160000 ){
                            $vip = $vip[0]["vip"] = 4;
                            $rt =Db::table('user')->where("username",$value)->update(['vip' => $vip]);
                        }else if($vip[0]["unused"] >= 160000 && $vip[0]["unused"] <= 220000 ){
                            $vip = $vip[0]["vip"] = 5;
                            $rt =Db::table('user')->where("username",$value)->update(['vip' => $vip]);
                        }else if($vip[0]["unused"] > 220000){
                            $vip = $vip[0]["vip"] = 6;
                            $rt =Db::table('user')->where("username",$value)->update(['vip' => $vip]);
                        }
                        if($rt){
                            return json_encode(['code' => 1, 'msg' => '恭喜，大哥充值成功']);
                        }
                    }
                } else {
                    return json_encode(['code' => -2, 'msg' => '大哥刚刚充值出错了，您可以在试试吗？']);
                }
            }
        }else{
            return json_encode(['code'=>-1 , 'msg'=>'对不起，您此次充值发生了错误。']);
        }
    }
    //消息显示
    public function news()
    {
        return view('news');
    }













    //退出
    public function tui_ok(Request $request)
    {
        $request->session()->forget('username');
        return redirect('/');
    }
}
